
/**
 * @desc 接送站设置，新增/修改接送站地址弹框
 * @author yd_zhaorongjun
 */
<template>
  <div>
    <el-dialog
      :title="title"
      class="gl_dialog_TitleLine gl_dl_footer"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="440px"
    >
      <el-form ref="Api_GET_PickUpAddressDto" v-loading="loading" class="gl_form_mini gl_dialog_height" :model="Api_GET_PickUpAddressDto" :rules="Api_GET_PickUpAddressDto_Rules" label-position="top" label-width="120px" style="padding:0 30px">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="接站地点名称" prop="departureStation">
              <el-input v-model="Api_GET_PickUpAddressDto.departureStation" placeholder="请输入" type="text" size="mini" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="送站地点名称" prop="arrivalStation">
              <el-input v-model="Api_GET_PickUpAddressDto.arrivalStation" placeholder="请输入" type="text" size="mini" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <!-- 取消按钮 -->
        <el-button size="mini" @click="dialogVisible = false">{{ $t('button.cancel') }}</el-button>
        <!-- 保存按钮 -->
        <el-button v-if="type == 'edit'" type="primary" :loading="submitLoading" size="mini" @click="saveEdit()">确 定</el-button>
        <!-- 保存按钮 -->
        <el-button v-else type="primary" :loading="submitLoading" size="mini" @click="saveNew()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import validator from '@/lib/validate/index.js';
import { SiSTKWEventsManagementPickUpAddressDto } from '@/lib/form/game/swaggerForm';
import {
  api_GET_EventsManagementPickUpAddressPickUpAddressPickUpAddressId
} from '@/extend/api/gameswaggerApi/app.js';

import { CopyData } from '@/lib';

export default {
  props: {
    district: {
      type: Array,
      default() {
        return [];
      }
    },
    // 字典信息
    dict: {
      type: Object,
      default () {
        return {};
      }
    },
    dictAll: {
      type: Object,
      default () {
        return {};
      }
    }
  },
  data() {
    return {
      Api_GET_PickUpAddressDto: new SiSTKWEventsManagementPickUpAddressDto(),
      Api_GET_PickUpAddressDto_Rules: { // 必填项，验证密码格式
        departureStation: [
          validator.required('始发站名称不能为空')
        ],
        arrivalStation: [
          validator.required('到达站名称不能为空')
        ]
      },
      title: '',
      loading: false,
      submitLoading: false,
      dialogVisible: false,
      type: '', // 弹框状态，是新增和是修改 new 是新增，edit是修改
      rowId: '' // 编辑时候的id
    };
  },
  computed: {
  },
  watch: {
    dialogVisible(val) {
      if (!val) {
        this.$refs['Api_GET_PickUpAddressDto'].resetFields();
      }
    }
  },
  methods: {
    // 根据用户id获取信息
    getPickUpAddressId() {
      api_GET_EventsManagementPickUpAddressPickUpAddressPickUpAddressId(this.rowId).then(res => {
        this.Api_GET_PickUpAddressDto = new SiSTKWEventsManagementPickUpAddressDto();
        CopyData(res, this.Api_GET_PickUpAddressDto);
        this.loading = false;
      }, error => {
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
        this.loading = false;
      });
    },
    open(row, type) {
      this.loading = true;
      this.type = type;
      this.rowId = row ? row.id : '';
      this.title = type === 'edit' ? '编辑接送站地点' : '添加接送站地点';
      if (type === 'edit') {
        this.getPickUpAddressId();
      } else {
        this.Api_GET_PickUpAddressDto = new SiSTKWEventsManagementPickUpAddressDto();
        this.loading = false;
      }
      this.$nextTick(() => {
        this.dialogVisible = true;
      });
    },
    // 新增
    saveNew() {
      console.log(this.Api_GET_PickUpAddressDto);
      const returnData = new SiSTKWEventsManagementPickUpAddressDto();
      CopyData(this.Api_GET_PickUpAddressDto, returnData);
      this.$refs.Api_GET_PickUpAddressDto.validate(valid => {
        this.submitLoading = true;
        if (valid) {
          this.$emit('insert', returnData);
        } else {
          this.submitLoading = false;
          return false;
        }
      });
    },
    // 修改
    saveEdit() {
      console.log(this.Api_GET_PickUpAddressDto);
      const returnData = new SiSTKWEventsManagementPickUpAddressDto();
      CopyData(this.Api_GET_PickUpAddressDto, returnData);
      this.$refs.Api_GET_PickUpAddressDto.validate(valid => {
        this.submitLoading = true;
        if (valid) {
          this.$emit('update', returnData);
        } else {
          this.submitLoading = false;
          return false;
        }
      });
    }
  }
};
</script>
